/***********************************************************
 * Product Name: ExcelQuicker
 * Copyright (c)2005, Eunge
 * Author: Eunge
 * Web Site: www.eudev.net
 * Mail/MSN: eudev.net@yeah.net
 * ********************************************************/
using System;

namespace ExcelQuicker.Framework
{
	/// <summary>
	/// Conditional style class. You can set conditional style on some cells. If the value of these cells is suitable for the condition, they will be set style automatically.
	/// </summary>
	/// <remarks>You output 10 numbers. Before output, you cannot know the value of them. But you know if the value is more than 10, it will display red on excel document. Thereby, you can use EQConditionalStyle in this case.</remarks>
	[Serializable]
	public class EQConditionalRangeStyle: EQStyleBase  
	{
		private EQEventFormatConditionHandler mBeforeOperationEvent;
		private EQEventFormatConditionHandler mAfterOperationEvent;
		private Excel.XlFormatConditionOperator mOperator = Excel.XlFormatConditionOperator.xlEqual;
		private Excel.XlFormatConditionType mType = Excel.XlFormatConditionType.xlCellValue;
		private string mFormula1 = "";
		private string mFormula2 = "";
		private EQRangeStyle mRangeStyle;

		/// <summary>
		/// Set/Get specialized style which will be displayed when the value of cell is suitable
		/// </summary>
		public EQRangeStyle RangeStyle
		{
			get
			{
				return this.mRangeStyle;
			}
			set
			{
				this.mRangeStyle = value;
			}
		}

		/// <summary>
		/// Raised before operation
		/// </summary>
		public new EQEventFormatConditionHandler BeforeOperationEvent
		{
			get
			{
				return this.mBeforeOperationEvent;
			}
			set
			{
				this.mBeforeOperationEvent = value;
			}
		}

		/// <summary>
		/// Raised after operation
		/// </summary>
		public new EQEventFormatConditionHandler AfterOperationEvent
		{
			get
			{
				return this.mAfterOperationEvent;
			}
			set
			{
				this.mAfterOperationEvent = value;
			}
		}

		/// <summary>
		/// Forwarder expression of the value
		/// </summary>
		/// <example>
		/// <br>etc A&lt;value&lt;B</br>
		/// <br>A is forwarder expression</br>
		/// </example>
		public string Formula1
		{
			get
			{
				return this.mFormula1;
			}
			set
			{
				this.mFormula1 = value;
			}
		}

		/// <summary>
		/// Rear expression of the value
		/// </summary>
		/// <example>
		/// <br>etc A&lt;value&lt;B</br>
		/// <br>B is rear expression</br>
		/// </example>
		public string Formula2
		{
			get
			{
				return this.mFormula2;
			}
			set
			{
				this.mFormula2 = value;
			}
		}

		/// <summary>
		/// Set/Get the operator. Default value is EQFormatConditionOperator.Equal, stands for =
		/// </summary>
		public Excel.XlFormatConditionOperator Operator
		{
			get
			{
				return this.mOperator;
			}
			set
			{
				this.mOperator = value;
			}
		}

		/// <summary>
		/// Set/Get type of condition. Default value is EQFormatConditionType.CellValue
		/// </summary>
		public Excel.XlFormatConditionType Type
		{
			get
			{
				return this.mType;
			}
			set
			{
				this.mType = value;
			}
		}

		/// <summary>
		/// Output this object to excel document
		/// </summary>
		/// <param name="objRange"></param>
		protected internal override void Output(Excel.Range objRange)
		{
			objRange.FormatConditions.Delete();
			Excel.FormatCondition objFormatCondition = objRange.FormatConditions.Add(this.mType, this.mOperator, this.mFormula1, this.mFormula2);

			this.DoBeforeOperationEvent(objFormatCondition);	//Event

			if(this.RangeStyle != null)
				this.RangeStyle.Output(objFormatCondition.Font, objFormatCondition.Interior, objRange);

			this.DoAfterOperationEvent(objFormatCondition);	//Event
		}

		/// <summary>
		/// Execute BeforeOperationEvent delegate
		/// </summary>
		/// <param name="objFormatCondition"></param>
		protected void DoBeforeOperationEvent(Excel.FormatCondition objFormatCondition)
		{
			if(this.BeforeOperationEvent != null)
				this.BeforeOperationEvent(objFormatCondition);
		}

		/// <summary>
		/// Execute DoAfterOperationEvent delegate
		/// </summary>
		/// <param name="objFormatCondition"></param>
		protected void DoAfterOperationEvent(Excel.FormatCondition objFormatCondition)
		{
			if(this.AfterOperationEvent != null)
				this.AfterOperationEvent(objFormatCondition);
		}
	}
}
